﻿Imports System.IO

Public Class Inventory

#Region "Attributes"
    Private mInventoryItems() As InventoryItem
    Private mNumInventoryItems As Integer
#End Region

#Region "Properties"
    Public Event Inventory_InventoryItemReadFromFile(ByVal sender As Object, ByVal e As EventArgs)

    Public ReadOnly Property numInventoryItems As Integer
        Get
            Return mNumInventoryItems
        End Get
    End Property

    Public Property ith(ByVal i As Integer) As InventoryItem
        Get
            If i >= 0 And i < mNumInventoryItems Then
                Return mInventoryItems(i)
            Else
                Throw New IndexOutOfRangeException
            End If
        End Get
        Set(ByVal pvalue As InventoryItem)
            If i >= 0 And i < mNumInventoryItems Then
                mInventoryItems(i) = pvalue
            Else
                Throw New IndexOutOfRangeException
            End If
        End Set
    End Property
#End Region

#Region "Constructor"
    Public Sub New()
        ReDim mInventoryItems(0)
        mNumInventoryItems = 0
    End Sub
#End Region

#Region "Behavioral Methods"
    Public Overrides Function ToString() As String
        Return _toString()
    End Function

    Private Function _toString() As String
        Dim _tmpStr As String

        '_tmpStr = "( INVENTORY: " & "ID:" & _ID & ", Description:" & _Description & ", Purchase Cost:" & _PurchaseCost.ToString("N1") & ", Sale Price:" & _SalePrice & ", Quantity:" & _QuantityOnHand & " )"

        Return _tmpStr

    End Function

    Private Sub addInventoryItem(ByVal inventoryItem As InventoryItem)
        Dim newInventoryItem As InventoryItem = New InventoryItem(inventoryItem)

        ReDim Preserve mInventoryItems(mNumInventoryItems)
        mNumInventoryItems += 1
        Try
            ith(mNumInventoryItems - 1) = newInventoryItem
        Catch ex As IndexOutOfRangeException
            Throw ex
        End Try
    End Sub

    Public Sub _addInventoryItem(ByVal inventoryItem As InventoryItem)
        Dim newInventoryItem As InventoryItem = New InventoryItem(inventoryItem)
        If IsNothing(find(newInventoryItem.ID)) = False Then
            Throw New Inventory_InventoryItemAlreadyExists_Exception(newInventoryItem.ID)
            Exit Sub
        End If
        ReDim Preserve mInventoryItems(mNumInventoryItems)
        mNumInventoryItems += 1
        Try
            ith(mNumInventoryItems - 1) = newInventoryItem
        Catch ex As IndexOutOfRangeException
            Throw ex
        End Try
    End Sub

    Public Function find(ByVal inventoryItem As InventoryItem) As InventoryItem
        For i As Integer = 0 To mNumInventoryItems - 1
            If mInventoryItems(i).ID = inventoryItem.ID Then
                Return mInventoryItems(i)
            End If
        Next
        Return Nothing
    End Function

    Public Function find(ByVal id As String) As InventoryItem
        For i As Integer = 0 To mNumInventoryItems - 1
            If mInventoryItems(i).ID = id Then
                Return mInventoryItems(i)
            End If
        Next
        Return Nothing
    End Function

    Public Sub readMultiLineRecordsFromFile(ByVal pFileName As String)
        Dim inputFile As StreamReader
        Dim id As String
        Dim description As String
        Dim purchaseCost As Decimal
        Dim salePrice As Decimal
        Dim quantityOnHand As Integer
        Dim reorderPoint As Integer
        Dim reorderQuantity As Integer

        Try
            inputFile = New StreamReader(pFileName)
        Catch ex As Exception
            'Throw ex
            Throw New Inventory_ErrorReadingFromFile_Exception(pFileName)
        End Try 'New Streamreader

        Do While Not inputFile.EndOfStream
            id = inputFile.ReadLine
            description = inputFile.ReadLine
            Try
                purchaseCost = Decimal.Parse(inputFile.ReadLine)
                salePrice = Decimal.Parse(inputFile.ReadLine)
                quantityOnHand = Integer.Parse(inputFile.ReadLine)
                reorderPoint = Integer.Parse(inputFile.ReadLine)
                reorderQuantity = Integer.Parse(inputFile.ReadLine)
                Dim inventoryItem As New InventoryItem(id, description, purchaseCost, salePrice, quantityOnHand, reorderPoint, reorderQuantity)
                addInventoryItem(inventoryItem)
                RaiseEvent Inventory_InventoryItemReadFromFile(Me, New Inventory_InventoryItemReadFromFile_EventArgs(inventoryItem))
            Catch ex As Exception
                Throw New Inventory_ErrorReadingFromFile_Exception(pFileName)
            End Try
        Loop

        inputFile.Close()
    End Sub

    Public Sub readSingleLineRecordsFromFile(ByVal pFileName As String)
        Dim inputFile As StreamReader
        Dim id As String
        Dim description As String
        Dim purchaseCost As Decimal
        Dim salePrice As Decimal
        Dim quantityOnHand As Integer
        Dim reorderPoint As Integer
        Dim reorderQuantity As Integer
        Dim line As String
        Dim inventory As String()
        Try
            inputFile = New StreamReader(pFileName)
        Catch ex As Exception
            'Throw ex
            Throw New Inventory_ErrorReadingFromFile_Exception(pFileName)
        End Try 'New Streamreader
        Do While Not inputFile.EndOfStream
            line = inputFile.ReadLine
            inventory = line.Split(",")
            Try
                id = inventory(0)
                description = inventory(1)
                purchaseCost = Decimal.Parse(inventory(2))
                salePrice = Decimal.Parse(inventory(3))
                quantityOnHand = Integer.Parse(inventory(4))
                reorderPoint = Integer.Parse(inventory(5))
                reorderQuantity = Integer.Parse(inventory(6))
                Dim inventoryItem As New InventoryItem(id, description, purchaseCost, salePrice, quantityOnHand, reorderPoint, reorderQuantity)
                addInventoryItem(inventoryItem)
                RaiseEvent Inventory_InventoryItemReadFromFile(Me, New Inventory_InventoryItemReadFromFile_EventArgs(inventoryItem))
            Catch ex As Exception
                Throw New Inventory_ErrorReadingFromFile_Exception(pFileName)
            End Try
        Loop
        inputFile.Close()
    End Sub

    Public Function GenerateID() As String
        Dim random As New Random
        Dim id As Integer = random.Next(1, 10000)
        Return id.ToString()
    End Function
#End Region
End Class
